package animix.blue.server.user.mapper;


import animix.blue.server.common.bean.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    @Select("select * from tb_user where username = #{username}")
    public User getByUsername(String username);

    @SelectProvider(type = UserProvider.class, method = "list")
    public List<User> list(Map<String, Object> para);

    @Select("select * from tb_user where id = #{id}")
    public User getById(Integer id);

    @Select("select * from tb_user where phone = #{phone}")
    public User getByPhone(String phone);

    @Select("select * from tb_user where email = #{email}")
    public User getByEmail(String email);

    @Insert("insert into tb_user (username, password, phone, email, status, created, updated) values" +
            "(#{username}, #{password}, #{phone}, #{email}, #{status}, #{created}, #{updated})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = int.class)
    public void insert(User user);

    @Update("update tb_user set username = #{username}, phone = #{phone}, email = #{email}, status = #{status}, " +
            "created = #{created}, updated = #{updated} where id = #{id}")
    public void updateById(User user);

    @UpdateProvider(type = UserProvider.class, method = "updateByIdSelective")
    public void updateByIdSelective(User user);

    @Delete("delete from tb_user where id = #{id}")
    public void deleteById(Integer id);

    @Delete({
            "<script>",
            "delete from tb_user where id in",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    public void deleteByIds(@Param("ids") Integer[] ids);

    @UpdateProvider(type = UserProvider.class, method = "updateByIdsSelective")
    public void updateByIdsSelective(@Param("ids") Integer[] ids, @Param("user") User user);
}
